package com.wanmei.controller.category;

import com.wanmei.exception.WanmeiException;
import com.wanmei.pojo.Category;
import com.wanmei.pojo.JsonResult;
import com.wanmei.pojo.UITreeView;
import com.wanmei.service.Impl.CategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/category")
public class CategoryInfoController {
    @Autowired
    private CategoryServiceImpl categoryService;

    @RequestMapping("/showCategory")
    @ResponseBody
    public List<UITreeView> showCategory() {
        return categoryService.getParentNode();
    }


    /**
     * 删除分类
     *
     * @param cid
     * @return
     */
    @RequestMapping("/deleteCategory")
    @ResponseBody
    public JsonResult deleteCategory(@RequestParam("cid") int cid) {

        JsonResult result = new JsonResult();
        int code = categoryService.deleteCategoryByCid(cid);
        result.setCode(code);
        return result;
    }

    /**
     * 点击编辑 获取下拉框和数据
     */

    @RequestMapping("/getCategory")
    public ModelAndView getCategory(@RequestParam("cid") int cid,@RequestParam(value = "pageNum" ,defaultValue = "1")int pageNum) {
        ModelAndView mv = new ModelAndView();
        List<Category> list = categoryService.getAllChild(cid);
        Category category = categoryService.selectByPrimaryKey(cid);
        Category category1 =categoryService.selectByPrimaryKey(category.getParentid());
        //建立一个集合用来删除自己本身.
        List<Category> list1 =new ArrayList<>();
        for (Category category2:list){
            if (category2.getCid()==category.getCid()){
                list1.add(category2);
            }
        }
        list.removeAll(list1);
        mv.addObject("list", list);
        mv.addObject("category", category);
        mv.addObject("parentCategory",category1);
        mv.addObject("pageNum",pageNum);
        mv.setViewName("product/categoryEdit");
        return mv;
    }

    /**
     *验证是否分类名重复
     */
    @RequestMapping("/checkCname")
    @ResponseBody
    public JsonResult checkCname(@RequestParam("cname") String cname){
        JsonResult result = new JsonResult();
        int code=categoryService.countCname(cname);
        result.setCode(code);
        return  result;
    }

    @RequestMapping("/editCategory")
    @ResponseBody
    public JsonResult editCategory(Category category, HttpServletRequest req) throws WanmeiException {
        JsonResult result = new JsonResult();
        String parentid =req.getParameter("heard");
        System.out.println("parentidWei："+parentid);
        System.out.println("category为"+category);
        category.setParentid(Integer.parseInt(parentid));
        int code = categoryService.saveByPrimaryKeySelective(category);
        result.setCode(code);
        return  result;
    }

}
